﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadingScene : MonoBehaviour
{
    public Image progressUI;  //进度条
    public Text progressValue;       //进度值
    private AsyncOperation prog;    //异步加载对象
    string sceneName;

    void Start()
    {
        sceneName = GameManager.Instance.SceneName();
        Load();
    }

    public void Load()
    {
        prog = SceneManager.LoadSceneAsync(sceneName);  //异步加载场景

        StartCoroutine(LoadScene());
    }

    //设置滑动条
    private void setProgressValue(int value)
    {
        progressUI.fillAmount = (float)value / 100f;
        progressValue.text = value + "%";
    }


    private IEnumerator LoadScene()
    {
        prog.allowSceneActivation = false;  //如果加载完成，也不进入场景

        int toProgress = 0;
        int showProgress = 0;

        //测试了一下，进度最大就是0.9
        while (prog.progress < 0.9f)
        {
            toProgress = (int)prog.progress * 100;
            while (showProgress < toProgress)
            {
                showProgress += 1;
                setProgressValue(showProgress);
                yield return new WaitForEndOfFrame(); //等待一帧
            }
            //yield break;
        }
        //计算0.9---1   其实0.9就是加载好了，我估计真正进入到场景是1  
        toProgress = 99;
        while (showProgress < toProgress)
        {
            showProgress += 1;
            setProgressValue(showProgress);
            yield return new WaitForEndOfFrame(); //等待一帧
        }
        GameManager.Instance.nowState.state = StateOP.Next;
        prog.allowSceneActivation = true;  //如果加载完成，可以进入场景
    }

    /*
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    */
}
